//
//  SettlementViewController.m
//  何五路
//
//  Created by _ADY on 15/7/1.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "SettlementViewController.h"
#import "PayViewController.h"

@interface SettlementViewController ()

@end
static LoadingView *loading = nil;

#define nLString @"给商家留言（选填，45字以内。发票抬头可在此填写。）"
#define settlementhight  192/2
@implementation SettlementViewController
@synthesize allArray,mTableView,AddressesArray,AddressesDic,DicString;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"确认订单";
        self.view.backgroundColor = bgColor;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height-60) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    mTableView.hidden = YES;
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(treatedATask) name:kTLement object:nil];
    
    [self myBarView];
    
}

-(void)treatedATask
{
    [UIView animateWithDuration:0.5 animations:^{
        
    } completion:^(BOOL finished) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
-(void)YhJViewArray:(NSMutableArray*)YhJArray
{
    NSString * PriceStr = nil;
    if (yHQdic.count != 0)
    {
        [yHQdic removeAllObjects];
    }
    yHQdic = [NSMutableDictionary dictionaryWithCapacity:10];
    
    for (NSDictionary* YhjDic in  YhJArray)
    {
        if (PriceStr == nil)
            PriceStr = [NSString stringWithFormat:@"%f",[[YhjDic objectForKey:@"CouponPrice"]floatValue]];
        else
            PriceStr = [NSString stringWithFormat:@"%f",[[YhjDic objectForKey:@"CouponPrice"]floatValue]+[PriceStr floatValue]];
        if([[NSString stringWithFormat:@"%@",[YhjDic objectForKey:@"CouponLevel"]] isEqualToString:@"1"])
        {
            [yHQdic setObject:[YhjDic objectForKey:@"Id"] forKey:@"platformCoupon"];
        }
        else if ([[NSString stringWithFormat:@"%@",[YhjDic objectForKey:@"CouponLevel"]] isEqualToString:@"2"])
        {
            [yHQdic setObject:[YhjDic objectForKey:@"Id"] forKey:[NSString stringWithFormat:@"coupon%@",[YhjDic objectForKey:@"VendorId"]]];
        }else
        {
            
        }
    }
    yHQmoney = [PriceStr floatValue];
    if (PriceStr != nil) {
        if ([PriceStr intValue] > 0) {
            YBInt = 0;
        }
    }
    combinedLabel.text = [NSString stringWithFormat:@"实付款:¥%.2f",moneyF+yFmoney-yHQmoney-YBInt];
}

-(void)myBarView
{
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, screenMySize.size.height-60, screenMySize.size.width, 60)];
    barView.backgroundColor = [UIColor colorWithWhite:0 alpha:.8];
    [self.view addSubview:barView];
    
    combinedLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, screenMySize.size.width*2/3-20, barView.frame.size.height)];
    combinedLabel.text = [NSString stringWithFormat:@"实付款:¥%.2f",moneyF+yFmoney-yHQmoney-YBInt];
    combinedLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+3];
    combinedLabel.textColor = [UIColor whiteColor];
    [barView addSubview:combinedLabel];
    

    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setFrame:CGRectMake(screenMySize.size.width*2/3, 0, screenMySize.size.width*1/3, barView.frame.size.height)];
    [addButton setBackgroundColor:[UIColor colorWithRed:238/255.0 green:84/255.0 blue:84/255.0 alpha:1]];
    [addButton setTitle:@"提交订单" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(settlementAction) forControlEvents:UIControlEventTouchUpInside];
    [barView addSubview:addButton];
    
}

#pragma mark 提交订单
-(void)settlementAction
{
     if ([[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"Name"]] isEqualToString:@"(null)"])
     {
         [SendService alerViewMessage:@"请选择收货地址!"];
         return;
     }
//    hidShippingAddressId=地址ID
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    if (yHQdic.count != 0) {
        [postDic1 addEntriesFromDictionary:yHQdic];
    }
    
    [postDic1 setObject:DicString forKey:@"hidShippingAddressId"];
     [postDic1 setObject:@"true" forKey:@"isPhone"];
    [postDic1 setObject:[NSString stringWithFormat:@"%.2f",YBInt] forKey:@"exchangeNum"];
    
    NSMutableArray *dic = [[allArray objectForKey:@"Entity"] objectForKey:@"Items"];
    NSString *idString = nil;
    for (int i = 0 ; i < dic.count; i ++) {
        if (idString == nil) {
            idString = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"Id"]];
        }else
            idString = [NSString stringWithFormat:@"%@,%@",idString,[[dic objectAtIndex:i] objectForKey:@"Id"]];
        
    }
    if (idString.length != 0) {
        [postDic1 setObject:idString forKey:@"hidShoppingCartItemIds"];
    }
    if (![nLtextView.text isEqualToString:nLString] || nLtextView.text.length != 0)
    {
        NSMutableArray *dic1 = [[allArray objectForKey:@"Entity"] objectForKey:@"VendorItems"];
        
        for (NSDictionary *dic3 in dic1)
        {
            NSString *vId = [NSString stringWithFormat:@"%@",[dic3 objectForKey:@"VendorId"]];
            [postDic1 setObject:nLtextView.text forKey:[NSString stringWithFormat:@"remark%@",vId]];
        }
    }
    NSLog(@"提交订单传输数据 %@",postDic1);
    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray = [SendService ConfirmOrder:postDic1];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"提交订单 %@",myArray);
            if ([[NSString stringWithFormat:@"%@",[myArray objectForKey:@"Result"]] isEqualToString:@"success"])
            {
                PayViewController *details = [[PayViewController alloc] init];
                details.hidesBottomBarWhenPushed = YES;
                details.allString = [[myArray objectForKey:@"Entity"] objectForKey:@"OrderTotal"];
                details.urlStr = [[myArray objectForKey:@"Entity"] objectForKey:@"PayUrl"];
                [self.navigationController pushViewController:details animated:YES];
                
                
            }
            else
                [SendService alerViewMessage:[myArray objectForKey:@"Message"]];
            [loading closeLoading];
        });
    });
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//     [self.navigationController popViewControllerAnimated:YES];
    if (buttonIndex == 1)
    {
        YBInt = [ssidField.text floatValue];
        [mTableView reloadData];
    }
    combinedLabel.text = [NSString stringWithFormat:@"实付款:¥%.2f",moneyF+yFmoney-yHQmoney-YBInt];
}

-(NSString *)htmlEntityDeco:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"$" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"¥" withString:@""];
    
    return string;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSLog(@"确认订单 %@",allArray);
    if (AddressesArray.count >0)
    {
        [mTableView reloadData];
        return;
    }
    YBInt = 0.00;
    YBtypeInt = 0.00;
    yHQmoney = 0.00;
    moneyF = 0.00;
    NSMutableArray *dic = [[allArray objectForKey:@"Entity"] objectForKey:@"Items"];
    for (int i = 0; i < dic.count; i++)
    {
        NSString *price = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"CustomerDiscountUnitPrice"]];
        price = [self htmlEntityDeco:price];
        
        
        NSString *price1 = [NSString stringWithFormat:@"%@",[[dic objectAtIndex:i] objectForKey:@"Quantity"]];
        
        moneyF = moneyF+[price1 floatValue]*[price floatValue];
    }
    
    NSMutableArray *dic1 = [[allArray objectForKey:@"Entity"] objectForKey:@"VendorItems"];
    YBtypeInt = [[NSString stringWithFormat:@"%@",[[allArray objectForKey:@"Entity"] objectForKey:@"ConsumptionIntegralNum"]] floatValue];
    yFmoney = 0.00;
    for (NSDictionary *dic3 in dic1)
    {
        NSString *vId = [NSString stringWithFormat:@"%@",[dic3 objectForKey:@"Freight"]];
        if([[vId substringToIndex:1] hasSuffix:@"¥"])
        {
            vId = [vId substringFromIndex:1];
        }
        yFmoney = yFmoney + [vId floatValue];
    }
    
    combinedLabel.text = [NSString stringWithFormat:@"实付款:¥%.2f",moneyF+yFmoney-yHQmoney-YBInt];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *ret2 = [SendService Addresses];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (AddressesArray !=nil) {
                [AddressesArray removeAllObjects];
                AddressesArray = nil;
            }
            AddressesArray = [[NSMutableArray alloc] init];
            
            for (NSDictionary *pic in [[ret2 objectForKey:@"Entity"] objectForKey:@"Addresses"])
            {
                [AddressesArray addObject:pic];
                if ([[NSString stringWithFormat:@"%@",[pic objectForKey:@"IsDefaultShippingAddress"]] isEqualToString:@"1"])
                {
                    AddressesDic = pic;
                    DicString = [NSString stringWithFormat:@"%@",[pic objectForKey:@"Id"]];
                }
            }
            mTableView.hidden = NO;
            [mTableView reloadData];
//            NSLog(@"收货地址列表 %@",ret2);
            
        });
    });
    
}
#pragma mark -
#pragma mark Table View DataSource

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (section == 0)
//        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2)
        return 2;
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 2 ||indexPath.section == 3||indexPath.section == 4||indexPath.section == 5)
    {
        return settlementhight*.7;
    }

    return settlementhight;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettlementCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.backgroundColor = [UIColor whiteColor];
    
    
    if (indexPath.section == 0)
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 1)
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 2)
        return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 3)
        return [self mCell:cell dCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 4)
        return [self mCell:cell eCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 6)
        return [self mCell:cell fCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 5)
        return [self mCell:cell gCTableView:aTableView cellForRowAtIndexPath:indexPath];
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    title = [title stringByReplacingOccurrencesOfString:@"<null>" withString:@""];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}

-(UILabel*)setFrame:(CGRect)frame set1Title:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    title = [title stringByReplacingOccurrencesOfString:@"<null>" withString:@""];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 2;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}
-(void)selectAddressesArray:(NSMutableArray*)addressArray
{
    if (AddressesArray !=nil) {
        [AddressesArray removeAllObjects];
        AddressesArray = nil;
    }
    AddressesArray = [[NSMutableArray alloc] init];
    AddressesArray = addressArray;
}
-(void)selectAddressID:(NSString*)addressId
{
    for (NSDictionary *pic in AddressesArray)
    {
        if ([[NSString stringWithFormat:@"%@",[pic objectForKey:@"Id"]] isEqualToString:[NSString stringWithFormat:@"%@",addressId]])
        {
            AddressesDic = pic;
            DicString = [NSString stringWithFormat:@"%@",[pic objectForKey:@"Id"]];
            [mTableView reloadData];
            return;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0)
    {
        AddressSelectViewController *details = [[AddressSelectViewController alloc] init];
        details.DicString =DicString;
        details.delegate =self;
        details.AddressesArray = AddressesArray;
        details.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:details animated:YES];
    }
    else if (indexPath.section == 1)
    {
        NSMutableArray *dic = [[allArray objectForKey:@"Entity"] objectForKey:@"Items"];
        SettlementAllViewController *details = [[SettlementAllViewController alloc] init];
        details.dic = dic;
        details.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:details animated:YES];
        
    }
    else if (indexPath.section == 4)
    {
        YhJViewController *details = [[YhJViewController alloc] init];
        details.dic = [allArray objectForKey:@"Entity"];
        details.hidesBottomBarWhenPushed = YES;
        details.delegate = self;
        [self.navigationController pushViewController:details animated:YES];
    }
    else if (indexPath.section == 5)
    {
         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"请输入元宝数量,可用元宝(%.2f)",YBtypeInt] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeDefault];
        [alertView show];
        
        ssidField = [alertView textFieldAtIndex:0];
        
        if (YBtypeInt >= moneyF+yFmoney-yHQmoney) {
            ssidField.text = [NSString stringWithFormat:@"%.2f",moneyF+yFmoney-yHQmoney];
        }
        else
            ssidField.text = [NSString stringWithFormat:@"%.2f",YBtypeInt];
        ssidField.delegate = self;
        YBInt = [ssidField.text floatValue];
        isHaveDian = YES;
    }
}

//textField.text 输入之前的值         string 输入的字符
- (BOOL)textField:(UITextField *)textField1 shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *text = [textField1.text stringByReplacingCharactersInRange:range withString:string];
    float type1 = [text floatValue];

    
    if ([textField1.text rangeOfString:@"."].location==NSNotFound &&[ssidField.text rangeOfString:@"."].location==NSNotFound) {
        isHaveDian = NO;
    }
    if ([string length]>0)
    {
        unichar single=[string characterAtIndex:0];//当前输入的字符
        if ((single >='0' && single<='9') || single=='.')//数据格式正确
        {
            //首字母不能为0和小数点
            if([textField1.text length]==0){
                if(single == '.'){
                    [textField1.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                    
                }
            }
            if (single=='.')
            {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian=YES;
                    if (type1 > YBInt) {
//                        ssidField.text = [NSString stringWithFormat:@"%.2f",YBInt];
                        return NO;
                    }
                    return YES;
                }else
                {
                    [textField1.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            else
            {
                if (isHaveDian)//存在小数点
                {
                    //判断小数点的位数
                    NSRange ran=[textField1.text rangeOfString:@"."];
                    int tt=range.location-ran.location;
                    if (tt <= 2){
                        if (type1 > YBInt) {
//                            ssidField.text = [NSString stringWithFormat:@"%.2f",YBInt];
                            return NO;
                        }
                        return YES;
                    }else
                    {
                        return NO;
                    }
                }
                else
                {
                    if (type1 > YBInt) {
//                        ssidField.text = [NSString stringWithFormat:@"%.2f",YBInt];
                        return NO;
                    }
                    return YES;
                }
            }
        }else{//输入的数据格式不正确
            [textField1.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    else
    {
        return YES;
    }
    
}

//- (BOOL)textField:(UITextField *)text1Field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    NSString *text = [text1Field.text stringByReplacingCharactersInRange:range withString:string];
//    float type1 = [text floatValue];
//    if (type1 > YBInt) {
//        ssidField.text = [NSString stringWithFormat:@"%.2f",YBInt];
//        return NO;
//    }
//    
//    return YES;
//}

- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, screenMySize.size.width,settlementhight)];
    [bgImageView setImage:[UIImage imageNamed:@"order_backg"]];
    [cell.contentView addSubview:bgImageView];
    
    
    UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,15, 20,20)];
    [bg1ImageView setImage:[UIImage imageNamed:@"order_name"]];
    [cell.contentView addSubview:bg1ImageView];
    
    UIImageView *bg2ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(150,15, 20,20)];
    [bg2ImageView setImage:[UIImage imageNamed:@"order_phone"]];
    [cell.contentView addSubview:bg2ImageView];
    
    if (![[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"Name"]] isEqualToString:@"(null)"])
    {
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(45,15, 100,20) setTitle:[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"Name"]] setFont:labelSize-1 setColor:[UIColor blackColor]]];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(175,15, screenMySize.size.width-195,20) setTitle:[NSString stringWithFormat:@"%@",[AddressesDic objectForKey:@"CellphoneNumber"]] setFont:labelSize-1 setColor:[UIColor blackColor]]];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(20,45, screenMySize.size.width-40,settlementhight-50-10) setTitle:[NSString stringWithFormat:@"%@%@%@%@%@",[AddressesDic objectForKey:@"StateProvinceName"],[AddressesDic objectForKey:@"CityName"],[AddressesDic objectForKey:@"DistrictName"],[AddressesDic objectForKey:@"StreetName"],[AddressesDic objectForKey:@"Address1"]] setFont:labelSize+1 setColor:[UIColor blackColor]]];
    }

    

    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *dic = [[allArray objectForKey:@"Entity"] objectForKey:@"Items"];
    if (dic.count > 1)
    {
        int Hight = 0;

        UIScrollView *ta1ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width,settlementhight)];
        ta1ScrollView.backgroundColor = [UIColor clearColor];
        [ta1ScrollView setPagingEnabled:NO];
        [ta1ScrollView setShowsHorizontalScrollIndicator:NO];
        [cell.contentView addSubview:ta1ScrollView];
        //解决UIScrollView把uitableviewcell的点击事件屏蔽start
        ta1ScrollView.userInteractionEnabled = NO;
        [ta1ScrollView addGestureRecognizer:ta1ScrollView.panGestureRecognizer];
        //解决UIScrollView把uitableviewcell的点击事件屏蔽end
        
        [ta1ScrollView setContentSize:CGSizeMake(self.view.bounds.size.width*dic.count/3,settlementhight)];
        
        for (int i = 0; i < dic.count; i ++)
        {
            
            UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+i*(settlementhight-15),Hight+10, settlementhight-20,settlementhight-20)];
            [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:i] objectForKey:@"Picture"]objectForKey:@"ImageUrl"]]]];
            bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
            //    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
            [ta1ScrollView addSubview:bgImageView];
            
            UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9.5+i*(settlementhight-15),Hight+9.5, settlementhight-19,settlementhight-19)];
            [bg1ImageView setImage:[UIImage imageNamed:@"gwckk"]];
            [ta1ScrollView addSubview:bg1ImageView];
        }

        
    }
    else
    {
        int Hight = 0;
        
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,Hight+10, settlementhight-20,settlementhight-20)];
        [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[[dic objectAtIndex:indexPath.row] objectForKey:@"Picture"]objectForKey:@"ImageUrl"]]]];
        bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
        //    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:bgImageView];
        
        UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9.5,Hight+9.5, settlementhight-19,settlementhight-19)];
        [bg1ImageView setImage:[UIImage imageNamed:@"gwckk"]];
        [cell.contentView addSubview:bg1ImageView];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(settlementhight, Hight+10, screenMySize.size.width-settlementhight-80, 50) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"ProductName"]] setFont:labelSize setColor:[UIColor blackColor]]];
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(settlementhight, Hight+10+50, screenMySize.size.width-settlementhight-80, 20) setTitle:[NSString stringWithFormat:@"x%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"Quantity"]] setFont:labelSize setColor:[UIColor blackColor]]];

        
        [cell.contentView addSubview:[self setFrame:CGRectMake(screenMySize.size.width-80, settlementhight/2-10, 80, 20) setTitle:[NSString stringWithFormat:@"%@",[[dic objectAtIndex:indexPath.row] objectForKey:@"CustomerDiscountUnitPrice"]] setFont:labelSize setColor:[UIColor redColor]]];

    }
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *aOrray = [NSArray arrayWithObjects:@"在线支付",@"顺丰快递",@"工作日，双休日与假日均可送货", nil];
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, (settlementhight*.7-40)/2, 70, 40) setTitle:[aOrray objectAtIndex:indexPath.row] setFont:labelSize setColor:[UIColor blackColor]]];
    
    NSArray *array = nil;
    if (indexPath.row == 0) {
        array = [NSArray arrayWithObjects:@"在线支付",@"顺丰快递",@"工作日，双休日与假日均可送货", nil];
    }
    else if (indexPath.row == 1) {
        array = [NSArray arrayWithObjects:@"电子发票",@"个人",@"非图书商品一一明细", nil];
    }
    for (int i = 0; i < array.count; i++)
    {
        [cell.contentView addSubview:[self setFrame:CGRectMake(80, i*settlementhight*.7/3, screenMySize.size.width-105, settlementhight*.7/3) set1Title:[array objectAtIndex:i] setFont:labelSize setColor:[UIColor blackColor]]];
    }
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell dCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryNone;
    if (nLtextView != nil) {
        [cell.contentView addSubview:nLtextView];
        return cell;
    }
    nLtextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, settlementhight)] ;
    nLtextView.backgroundColor = [UIColor clearColor];
    nLtextView.textColor = [UIColor grayColor];
    [nLtextView setFont:[UIFont systemFontOfSize:labelSize]];
    nLtextView.returnKeyType = UIReturnKeyDone;
    nLtextView.text = nLString;
    nLtextView.delegate = self;
    nLtextView.editable = YES;
    nLtextView.scrollEnabled = YES;
    [cell.contentView addSubview:nLtextView];
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell eCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, (settlementhight*.7-40)/2, screenMySize.size.width-30, 40) setTitle:@"优惠券" setFont:labelSize setColor:[UIColor blackColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(80, (settlementhight*.7-40)/2, screenMySize.size.width-105, 40) set1Title:[NSString stringWithFormat:@"-¥%.2f",yHQmoney] setFont:labelSize setColor:[UIColor redColor]]];
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell gCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell.contentView addSubview:[self setFrame:CGRectMake(10, (settlementhight*.7-40)/2, screenMySize.size.width-30, 40) setTitle:@"元宝" setFont:labelSize setColor:[UIColor blackColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(80, (settlementhight*.7-40)/2, screenMySize.size.width-105, 40) set1Title:[NSString stringWithFormat:@"-¥%.2f",YBInt] setFont:labelSize setColor:[UIColor redColor]]];
    
    return cell;
}


- (UITableViewCell *)mCell:(UITableViewCell*)cell fCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    NSArray *aOrray = [NSArray arrayWithObjects:@"商品金额",@"运费", nil];
    for (int i = 0; i < aOrray.count; i++)
    {
        [cell.contentView addSubview:[self setFrame:CGRectMake(10, i*settlementhight/2, 150, settlementhight/2) setTitle:[aOrray objectAtIndex:i] setFont:labelSize+2 setColor:[UIColor blackColor]]];
    }
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(160, 0, screenMySize.size.width-165, settlementhight/2) set1Title:[NSString stringWithFormat:@"¥%.2f",moneyF] setFont:labelSize setColor:[UIColor redColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(160, settlementhight/2, screenMySize.size.width-165, settlementhight/2) set1Title:[NSString stringWithFormat:@"＋¥%.2f",yFmoney] setFont:labelSize setColor:[UIColor redColor]]];
    return cell;
}

#pragma mark - textview delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    if (range.location < 45)
    {
        return YES;
    }
    else
        textView.text = [textView.text substringToIndex:[textView.text length] - 1];
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [mTableView setContentOffset:CGPointMake(0, 120) animated:YES];
    if (textView == nLtextView)
    {
        if ([textView.text  isEqualToString:nLString] ) {
            textView.text = @"";
        }
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [mTableView setContentOffset:CGPointMake(0, 0) animated:YES];
    if (textView.text.length == 0 &&textView == nLtextView)
    {
        nLtextView.text = nLString;
    }

    [textView resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
